<?php

namespace App\Http\Controllers\Api;

use App\Jobs\OrderSubmit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class CatchOrderController extends BaseController
{
    private $log;

    public function __construct()
    {
        $this->log = new Logger('pushCatchData');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/pushCatchData.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }

    //
    public function pushOrder(Request $Request)
    {
        $getData = file_get_contents('php://input');
        $this->log->info('input' . ($getData));
        // var_dump(1111111111);
        // $getData = '[{"tag":"101","msg_id":"41384524395499653940:0:101:1653277317:7052986961300030983","data":"{\"biz\":53,\"order_status\":2,\"order_type\":0,\"p_id\":4935699459342548314,\"pay_amount\":990,\"pay_time\":1653277317,\"pay_type\":1,\"s_ids\":[4935699459342548314],\"shop_id\":20922515}"}]';
        // $getData = '[{"tag":"110","msg_id":"52258734319910446940:0:110:1659960371:1461021254:7121523973505943043","data":"{\"biz\":53,\"order_status\":105,\"order_type\":0,\"p_id\":4964401991343785225,\"s_ids\":[4964401991343785225],\"shop_id\":39692353}"}]';
        //$getData = '[{"tag":"101","msg_id":"61938832550493756940:0:101:1660271874:293800340:7121523973505943043","data":"{\"biz\":53,\"order_status\":2,\"order_type\":0,\"p_id\":4965739405523883916,\"pay_amount\":100,\"pay_time\":1660271804,\"pay_type\":4,\"s_ids\":[4965739405523883916],\"shop_id\":39692353}"}]';
        // $getData = '[{"tag":"100","msg_id":"68637521073203866940:0:100:1660525720:130258437:7121523973505943043","data":"{\"biz\":53,\"create_time\":1660525720,\"order_status\":1,\"order_type\":0,\"p_id\":4966830237012573686,\"s_ids\":[4966830237012573686],\"shop_id\":39692353}"}]'; // 创建订单
        // 下单成功
        // $getData = '[{"tag":"110","msg_id":"68637521073203866940:0:110:1660525739:3686862970:7121523973505943043","data":"{\"biz\":53,\"order_status\":105,\"order_type\":0,\"p_id\":4966830237012573686,\"s_ids\":[4966830237012573686],\"shop_id\":39692353}"}]';
        $getDatas = json_decode($getData, true);
        $shop_id = 0;
        $pid = '';
        if (!empty($getDatas)) {
            foreach ($getDatas as $key => $v) {
                if (!empty($v['data']) && is_string($v['data'])) {

                    $arrays = json_decode($v['data'], true);
                    // var_dump($arrays);
                    // 判断下状态
                    $flag = true;
                    if (!empty($arrays['order_status'])) {
                        switch ($arrays['order_status']) {
                            case 2:
                                // 备货中
                                $flag = false;
                                break;
                            case 3:
                                //已发货
                                $flag = false;
                                break;
                            case 0:
                                // 待确认单
                                $flag = false;
                                break;
                            case 1:
                                $flag = false;
                                break;
                        }
                    }
                    // 备货中 和 待确认单 暂不处理
                    if (!$flag) {
                        continue;
                    }
                    $shop_id = !empty($arrays['shop_id']) ? $arrays['shop_id'] : '';
                    $pid = !empty($arrays['p_id']) ? $arrays['p_id'] : '';
                    if (!empty($shop_id) && !empty($pid)) {
                        $data = [
                            'shop_id' => $shop_id,
                            'pid' => $pid,
                            'type' => 'douyin'
                        ];
                        // 将数据存入到表中   防止遗漏 验证
                        $res = DB::table('douyin_catch_verific')->where(['pid' => $pid])->first();
                        if (!$res) {
                            $datas = [
                                'pid' => $pid,
                                'shop_id' => $shop_id,
                                'created_at' => date('Y-m-d H:i:s'),
                                'is_ver' => 1
                            ];
                            DB::table('douyin_catch_verific')->insert($datas);
                            // var_dump($data);
                            dispatch(new OrderSubmit($data, 'catchOrder', 60))->onQueue('TaskOrderSubmitDispose');
                        }
                    }
                }
            }
        }
        $str = '{"code":0,"msg":"success"}';
        $this->log->info('输入的数据' . $getData . '返回的参数' . $str);
        return $str;
    }

    public function upThree($ordernum = '', $type = 'catchOrder')
    {
        dispatch(new OrderSubmit($ordernum, $type))->onQueue('TaskOrderSubmitDispose');
        return 1;
    }
}
